package com.sushi.service.impl;

import com.sushi.enums.Type;
import com.sushi.repository.entity.TagRelationEntity;
import com.sushi.repository.mapper.TagRelationMapper;
import com.sushi.service.TagRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sushi.response.ResponseBO;
import com.sushi.response.ResponsePageBO;
import com.sushi.response.TagRelationResp;
import com.sushi.request.TagRelationReq;
import com.sushi.request.TagRelationPageReq;
import com.sushi.utils.*;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sushi.exception.BusinessException;
import java.util.ArrayList;
import java.util.List;

/**
 * 标签关系 服务实现类
 * @date 2023-05-02
 * @time 17:16:38
 * @author Administrator
 */
@Slf4j
@Service
public class TagRelationServiceImpl extends ServiceImpl<TagRelationMapper, TagRelationEntity> implements TagRelationService {

	@Autowired
	private TagRelationMapper tagRelationMapper;

    

    /**
     * 新增
     */
    @Override
    public ResponseBO<TagRelationResp> add(TagRelationReq req) {
        TagRelationEntity entity = BeanUtil.convert(req, TagRelationEntity.class);
        if (ObjectUtil.isEmpty(entity)) {
            throw new BusinessException("保存失败");
        }
        entity.insert();
        return ResponseUtil.ok(BeanUtil.convert(entity, TagRelationResp.class));
    }

    /**
     * 逻辑删除
     */
    @Override
    public ResponseBO<TagRelationResp> logicRemove(TagRelationReq req) {
        if (StringUtil.isBlank(req.getId())) {
            throw new BusinessException("id不能为空");
        }
        TagRelationEntity entity = new TagRelationEntity();
        entity.setId(req.getId());
        entity.setDeleted(1L);
        entity.updateById();
        return ResponseUtil.ok(BeanUtil.convert(entity, TagRelationResp.class));
    }

    /**
     * 更新
     */
    @Override
    public ResponseBO<TagRelationResp> updateByField(TagRelationReq req) {
        if (ObjectUtil.isEmpty(req) || StringUtil.isBlank(req.getId())) {
            throw new BusinessException("必要参数不能为空");
        }
        TagRelationEntity entity = getById(req.getId());
        if (ObjectUtil.isEmpty(entity)) {
            return ResponseUtil.error("信息不存在，无法更新");
        }
        if (StringUtil.isNotBlank(req.getId())) {
            entity.setId(req.getId());
        }
        if (StringUtil.isNotBlank(req.getTagId())) {
            entity.setTagId(req.getTagId());
        }
        if (StringUtil.isNotBlank(req.getTagType())) {
            entity.setTagType(req.getTagType());
        }
        if (StringUtil.isNotBlank(req.getObjId())) {
            entity.setObjId(req.getObjId());
        }
        if (StringUtil.isNotBlank(req.getObjType())) {
            entity.setObjType(req.getObjType());
        }

        entity.updateById();
        log.info("更新成功：{}", JSON.toJSONString(entity));
        return ResponseUtil.ok(BeanUtil.convert(entity, TagRelationResp.class));
    }

    /**
     * 列表
     */
    @Override
    public ResponsePageBO<TagRelationResp> listByCondition(TagRelationPageReq req) {
        if (ObjectUtil.isEmpty(req) || ObjectUtil.isEmpty(req.getPageNum()) || ObjectUtil.isEmpty(req.getPageSize())) {
            throw new BusinessException("必要参数不能为空");
        }
        LambdaQueryWrapper<TagRelationEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TagRelationEntity::getDeleted, 0L);
        wrapper.eq(StringUtil.isNotBlank(req.getId()), TagRelationEntity::getId, req.getId());
        wrapper.eq(StringUtil.isNotBlank(req.getTagId()), TagRelationEntity::getTagId, req.getTagId());
        wrapper.eq(StringUtil.isNotBlank(req.getTagType()), TagRelationEntity::getTagType, req.getTagType());
        wrapper.eq(StringUtil.isNotBlank(req.getObjId()), TagRelationEntity::getObjId, req.getObjId());
        wrapper.eq(StringUtil.isNotBlank(req.getObjType()), TagRelationEntity::getObjType, req.getObjType());


        Page<TagRelationEntity> page = page(new Page<>(req.getPageNum(), req.getPageSize()), wrapper);
        if (ObjectUtil.isEmpty(page) || CollectionUtil.isEmpty(page.getRecords())) {
            return ResponsePageUtil.ok(req, new ArrayList<>(), 0L);
        }
        List<TagRelationResp> convert = BeanUtil.convert(page.getRecords(), TagRelationResp.class);
        return ResponsePageUtil.ok(req, convert, page.getTotal());
    }

    /**
     * 详情
     */
    @Override
    public ResponseBO<TagRelationResp> detail(TagRelationReq req) {
        if (ObjectUtil.isEmpty(req) || StringUtil.isBlank(req.getId())) {
            throw new BusinessException("必要参数不能为空");
        }
        TagRelationEntity entity = getById(req.getId());
        if (ObjectUtil.isEmpty(entity)) {
            throw new BusinessException("对象不存在");
        }
        return ResponseUtil.ok(BeanUtil.convert(entity, TagRelationResp.class));
    }

    @Override
    public List<String> getTagIdListByObj(String objId, Type.Tag objType) {
        List<TagRelationEntity> relationList = list(new LambdaQueryWrapper<TagRelationEntity>().eq(TagRelationEntity::getDeleted, 0L)
                .eq(TagRelationEntity::getObjType, objType.getCode()).eq(TagRelationEntity::getObjId, objId));
        List<String> tagIdList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(relationList)) {
            relationList.forEach(r -> tagIdList.add(r.getTagId()));
        }
        return tagIdList;
    }
}